﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace SuperX.DataService.Abstract
{
    [StructLayout(LayoutKind.Sequential)]
    public struct DeviceAddress : IComparable<DeviceAddress>
    {
        public int Area;
        public int Start;
        public ushort DBNumber;
        public ushort DataSize;
        public ushort CacheIndex;
        public byte Bit;
        public DataType VarType;
        public ByteOrder ByteOrder;
        public string Addr;
        public string Name;

        /// <summary>
        /// 采集方式，只有0/1不能从以Byte的方式读取只能批量读取数字量，再组合成Byte
        /// </summary>
        public GatherType GatherType;

        public short Offset;

        public DeviceAddress(string name, int area, ushort dbnumber, ushort cIndex, int start, ushort size, byte bit, DataType type, string addr, ByteOrder order = ByteOrder.None, GatherType digital = GatherType.Analogue, short offset = 1)
        {
            Name = name;
            Area = area;
            DBNumber = dbnumber;
            CacheIndex = cIndex;
            Start = start;
            DataSize = size;
            Bit = bit;
            VarType = type;
            ByteOrder = order;
            Addr = addr;
            GatherType = digital;
            Offset = offset;
        }

        public static readonly DeviceAddress Empty = new DeviceAddress(string.Empty, 0, 0, 0, 0, 0, 0, DataType.SHORT, string.Empty);

        public int CompareTo(DeviceAddress other)
        {
            return this.Area > other.Area ? 1 :
                this.Area < other.Area ? -1 :
                this.DBNumber > other.DBNumber ? 1 :
                this.DBNumber < other.DBNumber ? -1 :
                this.Start > other.Start ? 1 :
                this.Start < other.Start ? -1 :
                this.Bit > other.Bit ? 1 :
                this.Bit < other.Bit ? -1 : 0;
        }
    }

    /// <summary>
    /// 采集的方式
    /// </summary>
    public enum GatherType
    {
        /// <summary>
        /// 模拟量
        /// </summary>
        Analogue = 0,

        /// <summary>
        /// 数字量值为0/1
        /// </summary>
        Digital = 1,

        /// <summary>
        /// 同时存在
        /// </summary>
        Both = 2,
    }
}